# -*- coding: utf-8 -*-
# @Time        :2025/2/26 14:00
# @Author      :文刀水寿
# @File        : 客户端.py
"""
 @Description :
"""
import socket
import json

HOST = '127.0.0.1'
PORT = 9999


def get_hint(status_code):
    hints = {
        200: "游戏结束",
        300: "建议尝试更大的数字",
        400: "建议尝试更小的数字",
        500: "请输入1-100之间的整数"
    }
    return hints.get(status_code, "未知状态")


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    print("已连接到服务器, 请开始游戏~~~")
    while True:
        try:
            guess = int(input("请输入1-100之间的数字："))
            if guess < 1 or guess > 100:
                print(" 输入必须为1-100之间的整数！")
                continue

            request = json.dumps({"guess": guess})
            s.send(request.encode())

            data = s.recv(1024).decode()
            response = json.loads(data)

            # 显示完整反馈
            print(f"\n状态码：{response['status']}")
            print(f"系统反馈：{response['message']}")
            print(f"建议：{get_hint(response['status'])}")
            print(f"你已经猜了：{response['attempts']}次")

            if response["status"] == 200:
                break

        except ValueError:
            print("输入无效，请重新输入！")
        except KeyError:
            print("收到异常响应，请重试！")
